package com.sutong.platform.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sutong.common.result.BaseResponse;
import com.sutong.common.result.ErrorCode;
import com.sutong.platform.annotation.AuthCheck;
import com.sutong.platform.common.DeleteRequest;
import com.sutong.platform.common.ResultUtils;
import com.sutong.platform.constant.UserConstant;
import com.sutong.platform.exception.BusinessException;
import com.sutong.platform.exception.ThrowUtils;
import com.sutong.platform.model.dto.interfaceparam.InterfaceParamAddRequest;
import com.sutong.platform.model.dto.interfaceparam.InterfaceParamQueryRequest;
import com.sutong.platform.model.dto.interfaceparam.InterfaceParamUpdateRequest;
import com.sutong.platform.model.entity.InterfaceParam;
import com.sutong.platform.model.entity.User;
import com.sutong.platform.model.vo.InterfaceParamVo;
import com.sutong.platform.service.InterfaceParamService;
import com.sutong.platform.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 接口参数信息的接口
 *
 * @author sutong
 */
@RestController
@RequestMapping("/interfaceParam")
@Slf4j
public class InterfaceParamController {

    @Resource
    private InterfaceParamService interfaceParamService;

    @Resource
    private UserService userService;

    // region 增删改查

    /**
     * 创建
     *
     * @param interfaceParamAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addInterfaceParam(@RequestBody @Validated InterfaceParamAddRequest interfaceParamAddRequest, HttpServletRequest request) {
        if (interfaceParamAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        InterfaceParam interfaceParam = BeanUtil.copyProperties(interfaceParamAddRequest, InterfaceParam.class);
        User loginUser = userService.getLoginUser(request);
        interfaceParam.setUserId(loginUser.getId());
        boolean result = interfaceParamService.save(interfaceParam);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(interfaceParam.getId());
    }

    /**
     * 删除
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteInterfaceParam(@RequestBody @Validated DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = userService.getLoginUser(request);
        long id = deleteRequest.getId();
        InterfaceParam oldInterfaceParam = interfaceParamService.getById(id);
        ThrowUtils.throwIf(oldInterfaceParam == null, ErrorCode.NOT_FOUND_ERROR);
        if (!oldInterfaceParam.getUserId().equals(user.getId()) && !userService.isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        boolean b = interfaceParamService.removeById(id);
        return ResultUtils.success(b);
    }

    /**
     * 更新（仅管理员）
     *
     * @param interfaceParamUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateInterfaceParam(@RequestBody @Validated InterfaceParamUpdateRequest interfaceParamUpdateRequest) {
        if (interfaceParamUpdateRequest == null || interfaceParamUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        InterfaceParam interfaceParam = BeanUtil.copyProperties(interfaceParamUpdateRequest, InterfaceParam.class);
        long id = interfaceParamUpdateRequest.getId();
        InterfaceParam oldInterfaceParam = interfaceParamService.getById(id);
        ThrowUtils.throwIf(oldInterfaceParam == null, ErrorCode.NOT_FOUND_ERROR);
        boolean result = interfaceParamService.updateById(interfaceParam);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<InterfaceParam> getInterfaceParamById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        InterfaceParam interfaceParam = interfaceParamService.getById(id);
        if (interfaceParam == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(interfaceParam);
    }

    /**
     * 分页获取列表（封装类）
     *
     * @param interfaceParamQueryRequest
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<InterfaceParamVo>> listInterfaceParamByPage(@RequestBody InterfaceParamQueryRequest interfaceParamQueryRequest) {
        Page<InterfaceParamVo> interfaceParamPages = interfaceParamService.pageWithChildren(interfaceParamQueryRequest);
        return ResultUtils.success(interfaceParamPages);
    }

    /**
     * 获取树型列表
     *
     * @return
     */
    @GetMapping("/list/tree")
    public BaseResponse<List<InterfaceParamVo>> listInterfaceParamWithTree(long interfaceInfoId, int type) {
        if (interfaceInfoId <= 0 || type < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<InterfaceParamVo> result = interfaceParamService.listWithTree(interfaceInfoId, type);
        return ResultUtils.success(result);
    }

    // endregion
}
